package com.lcj.demo.service;

import com.lcj.demo.domain.Department;
import com.lcj.demo.domain.Employee;
import com.lcj.demo.repository.EmployeeRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class EmployeeService {

    private final EmployeeRepository employeeRepository;
    public EmployeeService(EmployeeRepository employeeRepository){
        this.employeeRepository = employeeRepository;
    }
    public Employee saveOrUpdate(Employee employee) {
        return employeeRepository.save(employee);
    }

    @Transactional(readOnly = true) //只查询
    public List<Employee> findAll() {
        return employeeRepository.findAll( );
    }

    @Transactional(readOnly = true) //只查询
    public List<Employee> findAllBySearchKey(String searchKey) {
        return employeeRepository.findAllBySearchKey(searchKey);
    }

    public void delete(Long id) {
        employeeRepository.deleteById(id);
    }

    public Optional<Employee> findById(Long id){
       return  employeeRepository.findById(id);
    }

}
